﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FanPages.Server.Services.Contracts;
using FanPages.Server.Services;
using FanPages.Common;
using FanPages.Model;
using FanPages.Server.Services.Contracts.Dto;
using FanPages.Client.Core.Transformers;
using FanPagesWeb.Models;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;

namespace FanPagesWeb.Controllers
{
    public class FanItemController : Controller
    {

        IFanItemService fanItemService = new FanItemService();
        ITransformer<FanItem, FanItemDto> transformer = new FanItemTransformer();

        public ActionResult Index()
        {
            var fanItems = fanItemService.GetAll();

            return View(transformer.ToListEntity(fanItems));
        }

        public ActionResult SelectAndContinue([Bind(Exclude= "Image")] FanItem item)
        {
            Session["FanItem"] = item;
            return RedirectToAction("Index", "FixedLink");
        }
        //
        // GET: /FanItem/Details/5

        public ActionResult Details(int id)
        {
            return View(transformer.ToClient(fanItemService.GetById(id)));
        }

        //
        // GET: /FanItem/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FanItem/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Image")]FanItem item)
        {
            try
            {
                if (Request.Files.Count != 0)
                {
                    var image = ImageUtils.ImageToByteArray(Image.FromFile(Request.Files[0].FileName));
                    item.Image = image;
                }
                fanItemService.Save(transformer.ToDto(item));
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View(ex.Message);
            }
        }

        //
        // GET: /FanItem/Edit/5

        public ActionResult Edit(int id)
        {
            return View(transformer.ToClient(fanItemService.GetById(id)));
        }

        public ActionResult GetImage(int id)
        {
            var item = fanItemService.GetById(id);
            if (item.Image != null)
                return File(item.Image, "image/jpg");
            return null;
        }
        //
        // POST: /FanItem/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, [Bind(Exclude = "Image")]FanItem item)
        {
            try
            {
                if (Request.Files.Count != 0)
                {
                    var image = ImageUtils.ImageToByteArray(Image.FromFile(Request.Files[0].FileName));
                    item.Image = image;
                }
                fanItemService.Save(transformer.ToDto(item));

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /FanItem/Delete/5

        public ActionResult Delete(int id)
        {
            return View(transformer.ToClient(fanItemService.GetById(id)));
        }

        //
        // POST: /FanItem/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FanItem item)
        {
            try
            {
                fanItemService.Delete(transformer.ToDto(item));
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult UploadImage()
        {
            return View();
        }

        public ActionResult GetFanItemAutocomplete(string term)
        {
            var items = new FanItemTransformer().ToListEntity(fanItemService.SearchByName(term));
            List<JsonValue> json = new List<JsonValue>();
            foreach (var x in items)
            {
                json.Add(new JsonValue(){
                        label = x.Name,
                        value = x.Name,
                        id = x.Id.ToString(),
                        code = x.Code
                });
            }
            return Json(json, JsonRequestBehavior.AllowGet);
        }


    }

    class JsonValue
    {
        public string label { get; set; }
        public string value { get; set; }
        public string id { get; set; }
        public string code { get; set; }
    }
}
